﻿//-----------------------------------------------------------------------
// <copyright file="MonthlyUsage.cs" company="Redmist">
//     Copyright (c) Damien T Smith. All rights reserved.
// </copyright>
// <author>Damien Smith</author>
//-----------------------------------------------------------------------

namespace Redmist.UsageMonitor.UsageData
{
    using System;
    using System.Linq;

    /// <summary>Stores all of the raw data extracted from a monthly usage HTML page.</summary>
    public class MonthlyUsage
    {
        /// <summary>Initializes a new instance of the MonthlyUsage class.</summary>
        /// <param name="downloadStatistics">A Redmist.UsageMonitor.UsageData.UsageStatistics representing the basic download usage details.</param>
        /// <param name="uploadStatistics">A Redmist.UsageMonitor.UsageData.UsageStatistics representing the basic upload usage details.</param>
        /// <param name="anniversaryData">A Redmist.UsageMonitor.UsageData.AnniversaryData representing the anniversary details.</param>
        /// <param name="quota">The monthly quota in megabytes.</param>
        /// <param name="peakDownloadHistory">An array of integers representing the peak download history.</param>
        /// <param name="offPeakDownloadHistory">An array of integers representing the off peak download history.</param>
        /// <param name="peakUploadHistory">An array of integers representing the peak upload history.</param>
        /// <param name="offPeakUploadHistory">An array of integers representing the off peak upload history.</param>
        /// <param name="planName">A System.String representing the plan name.</param>
        /// <param name="excessRate">A System.String representing the rate at which excess usage is charged.</param>
        /// <param name="excessCharges">A System.String representing the excess charges for the current billing period.</param>
        public MonthlyUsage(UsageStatistics downloadStatistics, UsageStatistics uploadStatistics, AnniversaryData anniversaryData, int quota, int[] peakDownloadHistory, int[] offPeakDownloadHistory, int[] peakUploadHistory, int[] offPeakUploadHistory, string planName, string excessRate, string excessCharges)
        {
            this.DownloadStatistics = downloadStatistics;
            this.UploadStatistics = uploadStatistics;
            this.Quota = quota;
            this.AnniversaryData = anniversaryData;
            this.PeakDownloadHistory = peakDownloadHistory;
            this.OffPeakDownloadHistory = offPeakDownloadHistory;
            this.PeakUploadHistory = peakUploadHistory;
            this.OffPeakUploadHistory = offPeakUploadHistory;
            this.PlanName = planName;
            this.ExcessRate = excessRate;
            this.ExcessCharges = excessCharges;
        }

        public MonthlyUsage(Redmist.UsageMonitor.WebServiceProxy.DataUsageModel aanetUsageModel)
        {
            throw new System.NotImplementedException();
        }

        /// <summary>Gets a Redmist.UsageMonitor.UsageData.UsageStatistics representing the basic download usage details.</summary>
        public UsageStatistics DownloadStatistics { get; private set; }

        /// <summary>Gets a Redmist.UsageMonitor.UsageData.UsageStatistics representing the basic upload usage details.</summary>
        public UsageStatistics UploadStatistics { get; private set; }

        /// <summary>Gets a Redmist.UsageMonitor.UsageData.AnniversaryData representing the anniversary details.</summary>
        public AnniversaryData AnniversaryData { get; private set; }

        /// <summary>Gets the monthly quota in megabytes.</summary>
        public int Quota { get; private set; }

        /// <summary>Gets an array of integers representing the peak download history.</summary>
        public int[] PeakDownloadHistory { get; private set; }

        /// <summary>Gets an array of integers representing the off peak download history.</summary>
        public int[] OffPeakDownloadHistory { get; private set; }

        /// <summary>Gets an array of integers representing the peak upload history.</summary>
        public int[] PeakUploadHistory { get; private set; }

        /// <summary>Gets an array of integers representing the off peak download history.</summary>
        public int[] OffPeakUploadHistory { get; private set; }

        /// <summary>Gets a System.String representing the plan name.</summary>
        public string PlanName { get; private set; }

        /// <summary>Gets a System.String representing the rate at which excess usage is charged.</summary>
        public string ExcessRate { get; private set; }

        /// <summary>Gets a System.String representing the excess charges for the current billing period.</summary>
        public string ExcessCharges { get; private set; }


        public static UsageData.MonthlyUsage ConvertAanetUsageModelToMonthlyUsage(DataUsageModel m)
        {
            var daysToReportInHistory = m.DataByDay.Where(x => x.Day <= m.LastUsageUpdate);

            int daysOfDataAvailable = daysToReportInHistory.Count();
            int[] peakDownloadHistory = new int[daysOfDataAvailable];
            int[] offPeakDownloadHistory = new int[daysOfDataAvailable];
            int[] peakUploadHistory = new int[daysOfDataAvailable];
            int[] offPeakUploadHistory = new int[daysOfDataAvailable];
            int dayCounter=0;
            foreach (var dailyData in daysToReportInHistory.OrderBy(x => x.Day))
            {                
                peakDownloadHistory[dayCounter]=(int)Math.Round(dailyData.PeakDownloadedMB??0);
                peakUploadHistory[dayCounter] = (int)Math.Round(dailyData.PeakUploadedMB ?? 0);
                offPeakDownloadHistory[dayCounter] = (int)Math.Round(dailyData.OffpeakDownloadedMB ?? 0);
                offPeakUploadHistory[dayCounter] = (int)Math.Round(dailyData.OffpeakUploadedMB ?? 0);
                dayCounter++;
            }

            var monthlyUsage = new UsageData.MonthlyUsage(downloadStatistics: new UsageData.UsageStatistics(peak: (int)Math.Round(m.PeakDownloadsMB ?? 0),
                                                                                                                countedOffPeak: (int)Math.Round(m.CountedOffPeakDownloadsMB ?? 0)
                                                                                                                ),
                                                    uploadStatistics: new UsageData.UsageStatistics(peak: (int)Math.Round(m.PeakUploadsMB ?? 0),
                                                                                                                countedOffPeak: (int)Math.Round(m.CountedOffPeakUploadsMB ?? 0)
                                                                                                                ),
                                                    anniversaryData: new UsageData.AnniversaryData(usageLastUpdated: (m.LastUsageUpdate??DateTime.MinValue),
                                                                                                        anniversaryDay: (m.AnniversaryDay??0),
                                                                                                        nextAnniversary: (m.NextAnniversaryDate??DateTime.MinValue),
                                                                                                        lastAnniversary: (m.LastAnniversaryDate??DateTime.MinValue),
                                                                                                        timeRetrieved: DateTime.Now),
                                                    quota: (int)(m.PrepaidDataAllowanceMB ?? 0),
                                                    peakDownloadHistory: peakDownloadHistory,
                                                    offPeakDownloadHistory: offPeakDownloadHistory,
                                                    peakUploadHistory: peakUploadHistory,
                                                    offPeakUploadHistory: offPeakUploadHistory,
                                                    planName: m.ServiceDescription,
                                                    excessRate: (m.DataChargePerGB??0).ToString("C") + " per GB",
                                                    excessCharges: (m.ExcessChargeThisMonth??0).ToString("C"));
            return monthlyUsage;
        }
    }
}
